<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/component/pear/css/pear.css"/>
    <style>
        label.required:after { /* 给红色的必填项打红色星号（文字后）如果打前面用before*/
            content: ' *';
            color: red;
        }

        .layui-form-select dl {
            max-height: 260px;
        }
        .layui-disabled, .layui-disabled:hover{
            color: black !important;
        }
    </style>
</head>
<body class="pear-container">
<form class="layui-form" action="">
    <div class="layui-card">
        <div class="layui-card-body">
            <blockquote class="layui-elem-quote">归还档案名&nbsp;&nbsp;<span style="color: red" th:text="${fileName}"></span>&nbsp;&nbsp;默认是原位置,可以修改
            </blockquote>
            <div class="layui-form-item">
                <label class="layui-form-label" style="display:none">id</label>
                <div class="layui-input-block" style="display:none">
                    <input type="text" th:value="${id}" name="id" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">档案名称</label>
                <div class="layui-input-block">
                    <input type="text" name="fileName" required lay-verify="required" autocomplete="off"
                           th:value="${fileName}"
                           placeholder="请输入档案名称" disabled readonly
                           class="layui-input layui-disabled" lay-reqtext="档案名称为必填项">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">
                    归还部门
                </label>
                <div class="layui-input-block">
                    <input type="number" id="deptId" name="deptId" lay-verify="required"
                           lay-reqtext="归还部门为必填项"
                           style="display:none;width: 0px" autocomplete="off" class="layui-input">
                    <ul id="dataTree" class="dtree" data-id="0"></ul>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">归还人</label>
                <div class="layui-input-block">
<!--                    <select name="borrowers" lay-verify="required" lay-reqtext="借阅人为必填项" lay-search>-->
<!--                        <option value="">请选择归还人</option>-->
<!--                        <option th:each="item,stat:${myUserList}" th:value="${item.userName}"-->
<!--                                th:text="${item.userName}"></option>-->
<!--                    </select>-->
                    <input type="text" name="borrowers" required lay-verify="required" autocomplete="off"
                           th:value="${userName}"
                           disabled readonly
                           class="layui-input layui-disabled" lay-reqtext="归还人为必填项">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">归还日期</label>
                <div class="layui-input-block">
                    <input type="text" class="layui-input" autocomplete="off" lay-verify="required"
                           lay-reqtext="归还日期为必填项" name="borrowingDate" id="borrowingDate">
                </div>
            </div>
<!--            <div class="layui-form-item">-->
<!--                <label class="layui-form-label required">审批人</label>-->
<!--                <div class="layui-input-block">-->
<!--                    <select name="approver" lay-verify="required" lay-reqtext="审批人为必填项" lay-search>-->
<!--                        <option value="">请选择审批人</option>-->
<!--                        <option th:each="item,stat:${myUserList}" th:value="${item.userName}"-->
<!--                                th:text="${item.userName}"></option>-->
<!--                    </select>-->
<!--                </div>-->
<!--            </div>-->
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">用途</label>
                <div class="layui-input-block">
                            <textarea name="purpose" placeholder="请输入此文档的用途" class="layui-textarea"
                                      style="min-height: 65px!important;"></textarea>
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">备注</label>
                <div class="layui-input-block">
                            <textarea name="remarks" placeholder="请输入备注" class="layui-textarea"
                                      style="min-height: 65px!important;"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">选择层</label>
                <div class="layui-input-block">
                    <select name="layer" lay-verify="required" lay-search lay-reqtext="请选择层" lay-filter="layerChange">
                        <option value="">请选择层</option>
                        <option th:each="item,stat : ${layerMap}" th:value="${stat.current.key}"
                                th:text="${stat.current.key}" th:selected="${currentLayer} eq ${stat.current.key}">
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">选择列</label>
                <div class="layui-input-block">
                    <select name="column" lay-verify="required" lay-search lay-reqtext="请选择列" id="columnSelect">
                        <option value="">请选择列</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="returnFileSave">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </div>
    </div>
</form>
<script src="/component/layui/layui.js"></script>
<script src="/component/pear/pear.js"></script>
<script th:inline="javascript">
    var layerMap = [[${layerMap}]];
    var currentLayer = [[${currentLayer}]];
    var currentColumn = [[${currentColumn}]];
    var fileName = [[${fileName}]];
    var RFIDCheck = [[${RFIDCheck}]];
    var $ = layui.jquery;

    if (RFIDCheck == '0') {
        $("#columnSelect").removeAttr("lay-reqtext");
        $("#columnSelect").removeAttr("lay-verify");
    }


    layui.use(['form', 'jquery', 'laydate', 'dtree'], function () {
        let form = layui.form;
        let $ = layui.jquery;
        let laydate = layui.laydate;
        let dtree = layui.dtree;

        laydate.render({
            elem: '#borrowingDate' //指定元素
            , theme: '#393D49',
        });

        // 初始化树
        dtree.render({
            elem: "#dataTree",
            initLevel: "1",//默认展开层级为1
            width: "100%",
            method: 'get',
            dataStyle: "layuiStyle",  //使用layui风格的数据格式
            // response:{message:"msg",statusCode:200},  //修改response中返回数据的定义
            url: "/dept/buildDeptAll",
            dataFormat: "list",  //配置data的风格为list
            select: true, //指定下拉树模式
            selectTips: "请选择部门",
            selectCardHeight: "150"
        });
        dtree.on("node('dataTree')", function (obj) {
            var param = dtree.getNowParam("dataTree");
            $("#deptId").val(param.nodeId);
        });
        //第一次默认渲染
        columnSelect(layerMap[currentLayer])

        function columnSelect(columnList) {
            let insertHtml = '<option value="">请选择列</option>';
            for (let column in columnList) {
                if (columnList[column] == currentColumn) {
                    insertHtml += '<option value="' + columnList[column] + '" selected>' + columnList[column] + '</option>';
                } else {
                    insertHtml += '<option value="' + columnList[column] + '">' + columnList[column] + '</option>';
                }

            }
            $("#columnSelect").html(insertHtml);
            form.render('select'); //刷新select选择框渲染
        }

        //动态渲染下拉框
        form.on('select(layerChange)', function (data) {
            let selectVal = data.value //得到被选中的值
            let columnList = layerMap[selectVal];
            columnSelect(columnList)
        });
        form.on('submit(returnFileSave)', function (data) {
            console.log(data.field)
            let loading = layer.load();
            $.ajax({
                url: '/file/manage/returnFile',
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: "application/json;charset=utf-8",
                type: 'post',
                success: function (result) {
                    layer.close(loading);
                    if (result.code == 200) {
                        layer.msg(result.message, {icon: 1, time: 3000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.layui.table.reload("valid");
                        });
                    } else {
                        layer.msg(result.message, {icon: 2, time: 3000});
                    }
                }
            })

            return false;
        })
    })
</script>
</body>
</html>